﻿using Microsoft.SharePoint;
using SpImageCasheSDK;

namespace SpImageCacheSolution.SPImageCacheEventReceiver
{
    public class SpImageCacheEventReceiver : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            if (properties.ListItem.Folder == null)
            {
                SpImageCacheData spImageCacheData = new SpImageCacheData(string.Format("{0}/{1}", properties.RelativeWebUrl, properties.ListItem.Url));

                EventFiringEnabled = false;

                SpImageCachePresetsManager.RunPresetActions(properties.ListItem, spImageCacheData.PresetName);

                SPFile spFileImageCache = properties.Web.GetFile(spImageCacheData.OriginalFilePath);

                if (spFileImageCache != null) 
                    properties.ListItem["OrigModified"] = spFileImageCache.Item["Modified"];

                EventFiringEnabled = true;

            }
        }
    }
}
